package worst.shopping.adapter;

import database.DataBaseKeys;
import worst.shopping.R;
import worst.shopping.WorstShoppingApplication;
import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ImageCursorAdapter extends SimpleCursorAdapter {

	public static class ViewHolder
	{
		public TextView productName;
		public TextView productDescription;
		public TextView productCount;
		public TextView productPriceTotal;
		public ImageView productIcon;
	}
	
    private Cursor _cursor;
    private Context _context;
    private int _layout;
    
	public ImageCursorAdapter(Context context, int layout, Cursor cursor,
			String[] from, int[] to) {
		super(context, layout, cursor, from, to);
		this._cursor = cursor;
		this._context = context;
		this._layout = layout;
	}

	public View getView(int pos, View inView, ViewGroup parent) {
		_cursor.moveToPosition(pos);

		ViewHolder viewHolder;
		if (inView == null) {
			LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			inView = inflater.inflate(_layout, null);
			
			viewHolder = new ViewHolder();
			viewHolder.productDescription = (TextView) inView.findViewById(R.id.list_item_description);
			viewHolder.productName = (TextView) inView.findViewById(R.id.list_item_name);
			viewHolder.productIcon = (ImageView) inView.findViewById(R.id.list_item_image);	  
			viewHolder.productCount = (TextView) inView.findViewById(R.id.list_item_count);
			viewHolder.productPriceTotal = (TextView) inView.findViewById(R.id.list_item_totalprice);
			
			inView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder)inView.getTag();
		}

		String productName = _cursor.getString(_cursor.getColumnIndex(DataBaseKeys.PRODUCT_NAME));
		String description = getDescriptionTag();
		String count = getCountTag();
		String totalprice = getTotalPriceTag(count);
		
		viewHolder.productDescription.setText(description);
		viewHolder.productCount.setText(count);
		viewHolder.productName.setText(productName);
		viewHolder.productPriceTotal.setText(totalprice);
			
		if(getViewBinder() != null)
			getViewBinder().setViewValue(viewHolder.productCount, _cursor, pos);
		
		byte[] productIconByteArray = _cursor.getBlob(_cursor.getColumnIndex(DataBaseKeys.PRODUCT_ICON));
		   
		if (productIconByteArray != null) {
			viewHolder.productIcon.setImageBitmap(BitmapFactory.decodeByteArray(productIconByteArray, 0, productIconByteArray.length));
		}
		else
		{
			viewHolder.productIcon.setImageBitmap(WorstShoppingApplication.DEFAULT_PRODUCT_IMAGE);
		}
		   
		return inView;
	}

	private String getCountTag() {
		int countIndex = _cursor.getColumnIndex(DataBaseKeys.PRODUCT_COUNT);
		
		String count = countIndex != -1 ? _cursor.getString(countIndex) : "";
	
		return count;
	}

	private String getTotalPriceTag(String count){
		int priceIndex = _cursor.getColumnIndex(DataBaseKeys.PRODUCT_PRICE);
		
		
		int amount = 0;
		try {
			amount = Integer.parseInt(count);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		float baseprice = priceIndex != -1 ? _cursor.getFloat(priceIndex) : 0;
		
		return amount == 0 ? "" : "" + (amount * baseprice);
	}
	
	private String getDescriptionTag() {
		String str;
		String price = _cursor.getString(_cursor.getColumnIndex(DataBaseKeys.PRODUCT_PRICE));
		String unit = _cursor.getString(_cursor.getColumnIndex(DataBaseKeys.PRODUCT_UNIT));		
		if(price.equals("") && unit.equals(""))
		{
			str = "No further Information";
		}
		else if(price.equals(""))
		{
			str = unit;
		}
		else if(unit.equals(""))
		{
			str = price;
		}
		else
		{
			str = price + " per " + unit;
		}
		
		return str;
	}
}
